package com.web.fsc.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.web.fsc.dao.IUserDAO;
import com.web.fsc.domain.User;
import com.web.fsc.service.IUserService;
@Service
@Cacheable
@Transactional
public class UserService implements IUserService{
	@Autowired
	private IUserDAO userDao;
	@Autowired
	private HttpServletRequest request;
	
	@Override
	public User Login(String username, String password) {
		// TODO Auto-generated method stub
		User user=new User();
		if (username.equals("admin")&&password.equals("admin")){
			user.setName("admin");
			user.setPassword("admin");
			user.setAdmin(true);
			return user;
		}
		user=userDao.findByName(username);
		if (user==null)
			return null;
		if (password.equals(user.getPassword()))
		{
			return user;
		}
		return null;
	}

	@Override
	public User Register(User user) {
		// TODO Auto-generated method stub
//		User user=new User();
//		user.setName(username);
//		user.setPassword(password);
		userDao.save(user);
		return user;
	}

	@Override
	public User Update(User user) {
		// TODO Auto-generated method stub
		userDao.update(user);
		return user;
	}

	@Override
	public void Logout() {
		// TODO Auto-generated method stub
		request.getSession().removeAttribute("user");
	}

	@Override
	public User findByName(String name) {
		// TODO Auto-generated method stub
		return userDao.findByName(name);
	}

	@Override
	public List<User> listAll() {
		// TODO Auto-generated method stub
		return userDao.findAll(User.class);
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		userDao.delete(User.class, id);
	}

}
